/*
* Copyright 2015 2dfire.com All right reserved. This software is the
* confidential and proprietary information of 2dfire.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with 2dfire.com.
*/
package com.twodfire.merchant.poi;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;

/**
 * @author guipi
 * @description
 * @date 2017/7/8
 * ----------重大变更记录------------
 */
public abstract class AbstractConfig {

    Collection<String> arrayKeys;
    Collection<String> objectKeys;

    public Collection<String> getArrayKeys() {
        return arrayKeys;
    }

    public void setArrayKeys(Collection<String> arrayKeys) {
        this.arrayKeys = arrayKeys;
    }

    public Collection<String> getObjectKeys() {
        return objectKeys;
    }

    public void setObjectKeys(Collection<String> objectKeys) {
        this.objectKeys = objectKeys;
    }

    /**
     * 根据类型生成keys和对应的属性方法
     * @param clazz 类型
     * @param keys 需要生成的key
     * @param methodMap 对应的方法
     * @param methodPrefix 方法前缀
     * @throws IntrospectionException
     */
    void generatorKeyAndMethod(Class<?> clazz, Collection<String> keys, Map<String, Method> methodMap,String methodPrefix) throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz,Object.class);
        MethodDescriptor[] ms = beanInfo.getMethodDescriptors();
        for(MethodDescriptor m:ms){
            if(m.getName().startsWith(methodPrefix)){
                String key = m.getName().substring(methodPrefix.length());
                key = key.substring(0,1).toLowerCase()+key.substring(1);
                keys.add(key);
                methodMap.put(key,m.getMethod());
            }
        }
    }

    /**
     * 根据类型获得对应的方法
     * @param clazz 类型
     * @param keys 包含的属性
     * @param methodMap 对应的方法
     * @param methodPrefix 方法前缀
     * @throws IntrospectionException
     */
    void generatorMethod(Class<?> clazz, Collection<String> keys, Map<String, Method> methodMap, String methodPrefix) throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz,Object.class);
        MethodDescriptor[] ms = beanInfo.getMethodDescriptors();
        for(MethodDescriptor m:ms){
            if(m.getName().startsWith(methodPrefix)){
                String key = m.getName().substring(methodPrefix.length());
                key = key.substring(0,1).toLowerCase()+key.substring(1);
                if(keys.contains(key)){
                    methodMap.put(key,m.getMethod());
                }
            }
        }
    }
}
